/*
 * Copyright (c) Shenzhen 7th Road Technical Co., Ltd
 * Technical Support dept.
 * http://www.7road.com
 */
package sz7thrd.ts.framework;

import java.util.Date;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

/**
 * @author able
 * @date 2011-11-10
 * ===============================
 * 
 * 
 */
public class TimeServerHandler extends IoHandlerAdapter {

	@Override
	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception {
		cause.printStackTrace();
	}

	// 这个方法是目前这个类里最主要的，
	// 当接收到消息，只要不是quit，就把服务器当前的时间返回给客户端
	// 如果是quit，则关闭客户端连接
	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		
		String str = message.toString();
		if (str.trim().equalsIgnoreCase("quit")) {
			session.close();
			return;
		}

		Date date = new Date();
		session.write(date.toString());
		System.out.println("Message written..."+str);

	}
}
